package com.wensi.lib_voice.voiceEngine

import android.util.Log
import com.wensi.lib_voice.impl.OnNluResultListener
import com.wensi.lib_voice.words.NluWords
import org.json.JSONArray
import org.json.JSONObject

/**
 * 语音引擎分析
 */
object VoiceEngineAnalyze {

    private lateinit var onNluResultListener: OnNluResultListener // 语义接口
    // 分析结果 : 用户说的话
    fun analyzeNlu (nlu: JSONObject, onNluResultListener: OnNluResultListener) {
        this.onNluResultListener = onNluResultListener
        val rawText = nlu.optString("raw_text")

        // 解析results
        val results: JSONArray? = nlu.optJSONArray("results") ?: return
        when (results?.length()) {
            0 -> return
            1 -> analyzeNluSingle(results[0] as JSONObject) // 单条命中
            else -> {
                // 多条命中
            }
        }
    }

    // 分析单条结果
    private fun analyzeNluSingle(result: JSONObject) {
        Log.i("IntelligentVoice", "analyzeNluSingle分析单条结果 $result")
        val domain = result.optString("domain")
        val intent = result.optString("intent")
        val slots = result.optJSONObject("slots")

        Log.i("IntelligentVoice", "analyzeNluSingle分析单条结果domain $domain")
        when(domain) {
            NluWords.NLU_APP -> {
                when(intent) {
                    NluWords.INTENT_OPEN_APP -> {
                        // 打开app
                        val userAppName = slots.optJSONArray("user_app_name")
                        if (userAppName.length() > 0) {
                            val obj = userAppName[0] as JSONObject
                            val word = obj.optString("word")
                            onNluResultListener.openApp(word)
                        } else {
                            onNluResultListener.nluErr()
                        }
                    }
                    NluWords.INTENT_UNINSTALL_APP -> {
                        // 打开app
                        val userAppName = slots.optJSONArray("user_app_name")
                        if (userAppName.length() > 0) {
                            val obj = userAppName[0] as JSONObject
                            val word = obj.optString("word")
                            onNluResultListener.unInstallApp(word)
                        } else {
                            onNluResultListener.nluErr()
                        }
                    }
                    NluWords.INTENT_UPDATE_APP,
                    NluWords.INTENT_DOWNLOAD_APP,
                    NluWords.INTENT_SEARCH_APP,
                    NluWords.NLU_SEARCH,
                    NluWords.INTENT_SEARCH,
                    NluWords.INTENT_RECOMMEND_APP->{
                        // 其他app操作
                        val userAppName = slots.optJSONArray("user_app_name")
                        if (userAppName.length() > 0) {
                            val obj = userAppName[0] as JSONObject
                            val word = obj.optString("word")
                            onNluResultListener.otherAppHandle(word)
                        } else {
                            onNluResultListener.nluErr()
                        }

                    }
                    else -> {
                        onNluResultListener.nluErr()
                    }
                }

            }
            else ->{

            }
        }
    }
}